<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>

          <a-col :span="12">
            <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceName">
              <a-input v-model="model.deviceName" placeholder="请输入设备名称"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceType">
              <j-dict-select-tag type="list" v-model="model.deviceType" dictCode="wcs_device_type" placeholder="请选择设备类型" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="坐标" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="location">
              <a-input v-if="model.deviceType == 3" v-model="model.location" @input="validateInputLifter(model.location)"  placeholder="请输入坐标"  ></a-input>
              <a-input v-else v-model="model.location" @input="validateInput(model.location)"  placeholder="请输入坐标"  ></a-input>

            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row v-if="model.deviceType == 3">
          <a-col :span="12">
            <a-form-model-item label="提升机两侧坐标1" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leftLocation">
              <a-input v-model="model.leftLocation" @input="validateInputLifter(model.leftLocation)"  placeholder="提升机两侧坐标1"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="提升机两侧坐标2" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rightLocation">
              <a-input v-model="model.rightLocation" @input="validateInputLifter(model.rightLocation)" placeholder="提升机两侧坐标2"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row v-if="model.deviceType == 3">
          <a-col :span="12">
            <a-form-model-item label="提升机开始层" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startFloor">
              <a-input-number v-model="model.startFloor"   placeholder="请输入提升机开始层"  ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="提升机结束层" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endFloor">
              <a-input-number v-model="model.endFloor"  placeholder="请输入提升机结束层"  ></a-input-number>
            </a-form-model-item>
          </a-col>
        </a-row>


      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'WcsDeviceInfoForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          deviceName: [
            {required: true, message: '请输入设备名!'},
          ],
          deviceType: [
            {required: true, message: '请输入设备类型!'},
          ],
          location: [
            {required: true, message: '请输入设备坐标!'},
          ],
          leftLocation: [
            {required: true, message: '请输入提升机两侧坐标1!'},
          ],
          rightLocation: [
            {required: true, message: '请输入提升机两侧坐标2!'},
          ],
          startFloor: [
            {required: true, message: '请输入开始层!'},
          ],
          endFloor: [
            {required: true, message: '请输入结束层!'},
          ]
        },
        url: {
          add: "/wcs/wcsDeviceInfo/add",
          edit: "/wcs/wcsDeviceInfo/edit",
          queryById: "/wcs/wcsDeviceInfo/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      validateInput(coord) {
        const regex = /^(\([0-9]+,[0-9]+,[0-9]+\))$/; // 正则表达式，匹配格式 (x,y,z)
        if (!regex.test(coord)) {
          // 如果输入不匹配正则表达式，弹出提示窗体
          this.$message.error('坐标格式有误')
        }else {
          this.$message.success('坐标格式正确')
        }
      },
      validateInputLifter(coord) {
        const regex = /^(\([0-9]+,[0-9]+\))$/; // 正则表达式，匹配格式 (x,y,z)
        if (!regex.test(coord)) {
          // 如果输入不匹配正则表达式，弹出提示窗体
          this.$message.error('坐标格式有误')
        }else {
          this.$message.success('坐标格式正确')
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
    }
  }
</script>